package cn.tianjindong.blog_portal.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tianjindong.blog.common.pojo.PageView;
import cn.tianjindong.blog.mapper.BlogNoticeMapper;
import cn.tianjindong.blog.pojo.BlogNotice;
import cn.tianjindong.blog.pojo.BlogNoticeExample;
import cn.tianjindong.blog.pojo.BlogNoticeExample.Criteria;
import cn.tianjindong.blog_portal.service.NoticeService;

@Service
public class NoticeServiceImpl implements NoticeService {

	@Autowired
	private BlogNoticeMapper noticeMapper;

	@Override
	public BlogNotice getNoticeById(Long noticeId) {
		BlogNotice notice = noticeMapper.selectByPrimaryKey(noticeId);
		return notice;
	}

	@Override
	public List<BlogNotice> getFourNotice() {
		List<BlogNotice> list = noticeMapper.getNoticeListLimitAndTab(0, 4);
		return list;
	}

	@Override
	public int getNoticeCount() {
		BlogNoticeExample example = new BlogNoticeExample();
		Criteria criteria = example.createCriteria();
		criteria.andTabEqualTo(1);//1表示有效，0表示无效
		int count = noticeMapper.countByExample(example);
		return count;
	}

	@Override
	public PageView<BlogNotice> getNoticePageView(Integer currentPage, Integer countItem) {
		// 查询时开始索引
		int startIndex = (currentPage - 1) * countItem;
		List<BlogNotice> list = noticeMapper.getNoticeListLimitAndTab(startIndex, countItem);
		// 获取数据库总条数
		int totalItem = noticeMapper.countByExample(new BlogNoticeExample());

		PageView<BlogNotice> pageView = new PageView<>();
		pageView.setCurrentPage(currentPage);
		pageView.setCountItem(countItem);
		pageView.setItems(list);
		pageView.setTotalItem(totalItem);

		return pageView;
	}

}
